using Autofac.Core;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace 人员管理
{
    public partial class Form1 : Form
    {
        public PersonManager personManager;
        public PersonUtils personUtils;
        private Form1Model model;

        public Form1(PersonManager _personManager, PersonUtils _personUtils, Form1Model _model)
        {
            InitializeComponent();

            this.personManager = _personManager;
            this.personUtils = _personUtils;
            this.model = _model;

            // 设置 TextBox 的 DataBindings 属性
            BindingViewModel(tb_name, model, "Text", "Name");
            BindingViewModel(tb_age, model, "Text", "Age");
            BindingViewModel(cb_sex, model, "Text", "Sex");
            BindingViewModel(tb_addr, model, "Text", "Addr");
            BindingViewModel(dgv1, model, "DataSource", "Persons");
        }



        private void BindingViewModel(Control control, BaseModel model, string v1, string v2)
        {
            control.DataBindings.Add(v1, model, v2);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RefreshList();
            InitUI();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show($"{name},{age},{sex},{addr}");
            bool b = personManager.InsertPerson(model.Name, model.Age, model.Sex, model.Addr);
            if (b)
            {
                RefreshList();
                ClearEddit();
                //MessageBox.Show("插入成功");
            }
        }

        private void InitUI()
        {
            dgv1.Columns["id"].HeaderText = "ID";
            dgv1.Columns["name"].HeaderText = "姓名";
            dgv1.Columns["age"].HeaderText = "年龄";
            dgv1.Columns["sex"].HeaderText = "性别";
            dgv1.Columns["addr"].HeaderText = "地址";
            dgv1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void ClearEddit()
        {
            tb_name.Text = "";
            tb_age.Text = "";
            cb_sex.Text = "";
            tb_addr.Text = "";
        }

        private void RefreshList()
        {
            model.Persons = personUtils.SelectAll();
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            bool b = personUtils.EditPerson(model.SelId, model.Name, model.Age, model.Sex,model.Addr);
            if (b)
            {
                RefreshList();
            }
        }


        /// <summary>
        /// 选中一条记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv1_SelectionChanged(object sender, EventArgs e)
        {
            int count = dgv1.SelectedRows.Count;
            if (count > 1)
            {
                MessageBox.Show($"你选择了{count}条记录，只能选择一条");
            }
            else if (count == 1)
            {
                DataGridViewRow row = dgv1.SelectedRows[0];
                if (row != null)
                {
                    foreach (DataGridViewCell c in row.Cells)
                    {
                        int index = c.ColumnIndex;
                        object value = c.Value;
                        if (value != null)
                        {
                            if (index == 0)
                            {
                                model.SelId = Convert.ToInt32(c.Value);
                            }
                            else if (index == 1)
                            {
                                model.Name = Convert.ToString(value);
                            }
                            else if (index == 2)
                            {
                                model.Age = c.Value.ToString();
                            }
                            else if (index == 3)
                            {
                                model.Sex = c.Value.ToString();
                            }
                            else if (index == 4)
                            {
                                model.Addr = c.Value.ToString();
                            }
                        }
                    }
                }
            }

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            bool b = personUtils.DeletePerson(model.SelId);
            if (b)
            {
                RefreshList();
            }
        }

    }

    

}
